package com.suixun.controller;


import javax.annotation.Resource;

import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.suixun.controller.base.BaseController;
import com.suixun.domain.NewsCenter;
import com.suixun.service.NewsCenterService;

/**
 * <p>
 * 新闻中心 前端控制器
 * </p>
 *
 * @author jacky
 * @since 2018-01-11
 */
@RestController
@RequestMapping("/newsCenter")
public class NewsCenterController extends BaseController{
	
	@Resource
	private NewsCenterService newsCenterService;
	
	@PostMapping(value = "/add")
	public Object addNewsCenter(NewsCenter newsCenter, Boolean isTop) {
		newsCenterService.insertNewsCenter(newsCenter, isTop);
		return success();
	}

	@GetMapping(value = "/page/{page}/{size}")
	public Object page(@PathVariable Integer page,@PathVariable Integer size, String title, Integer type, Integer isShow) {
		PageInfo<NewsCenter> pageInfo = newsCenterService.pageNewsCenterExclusionContent(page, size, title, type, isShow);
		if(pageInfo != null && !CollectionUtils.isEmpty(pageInfo.getList())) {
			for(NewsCenter newsCenter : pageInfo.getList()) {
				newsCenter.setReadNum(newsCenterService.getReadNumById(newsCenter.getId()));
			}
		}
		return success(pageInfo);
	}

	@GetMapping(value = "/get/{id}")
	public Object page(@PathVariable Integer id) {
		return success(newsCenterService.getNewsCenter(id));
	}

	@PostMapping(value = "/update")
	public Object update(NewsCenter newsCenter, Boolean isTop) {
		newsCenterService.updateNewsCenter(newsCenter, isTop);
		return success();
	}

	@PostMapping(value = "/delete")
	public Object delete(String ids) {
		newsCenterService.deleteNewsCenter(ids);
		return success();
	}
}
